package com.lx.netty.fifth;

import com.lx.netty.forth.MyServerHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

public class WebSocketChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline cp = ch.pipeline();

        cp.addLast(new HttpServerCodec());
        cp.addLast(new ChunkedWriteHandler());
        cp.addLast(new HttpObjectAggregator(8192));
        cp.addLast(new WebSocketServerProtocolHandler("/ws"));


        cp.addLast(new IdleStateHandler(5, 7, 3, TimeUnit.SECONDS));
        cp.addLast(new MyServerHandler());

        cp.addLast(new TextWebSocketFrameHandler());
    }
}
